home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / SAS-C / sc655pch / guiprof / timerdev.c < prev   
Encoding:
C/C++ Source or Header  |  1995-01-26  |  1.6 KB  |  85 lines

  1. #include <proto/timer.h>
  2. #include <proto/exec.h>
  3. #include <proto/dos.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. struct Library *TimerBase;
  9. static struct timerequest TimerIO;
  10. static long E_Freq;
  11.  
  12. int _STITimerDev(void)
  13. {
  14.    struct EClockVal x;
  15.    if(OpenDevice(TIMERNAME, UNIT_ECLOCK, (struct IORequest *)&TimerIO, 0L))
  16.    {
  17.       return(-1);
  18.    }
  19.    TimerBase = (struct Library *)TimerIO.tr_node.io_Device;
  20.    E_Freq = ReadEClock(&x)/1000;  /* Set up E_Freq */
  21. }
  22.  
  23. void _STDTimerDev(void)
  24. {
  25.    if(TimerBase)
  26.    {
  27.       CloseDevice((struct IORequest *)&TimerIO);
  28.       TimerBase = NULL;
  29.    }
  30. }
  31.  
  32. long TimeDiff(struct EClockVal *first, struct EClockVal *second)
  33. {
  34.    /* For now, only use low four bytes. */
  35.    return (second->ev_lo - first->ev_lo)/E_Freq;
  36. }
  37.  
  38. #if DO_TEST
  39. int main(int argc, char *argv[])
  40. {
  41.    struct EClockVal time1, time2;
  42.    int delay = 50;
  43.    int i;
  44.    long loopover;
  45.    long timeover;
  46.  
  47.    if(argc > 1)
  48.       stcd_i(argv[1], &delay);
  49.    printf("Delay value is %ld\n", delay);
  50.  
  51.    ReadEClock(&time1);
  52.  
  53.    Delay(delay);
  54.  
  55.    ReadEClock(&time2);
  56.  
  57.    printf("Result is %ld milliseconds\n", TimeDiff(&time1, &time2));
  58.  
  59.    ReadEClock(&time1);
  60.  
  61.    for(i=0; i<100000; i++);
  62.  
  63.    ReadEClock(&time2);
  64.  
  65.    printf("Loop overhead is %ld\n", loopover=TimeDiff(&time1, &time2));
  66.  
  67.    ReadEClock(&time1);
  68.  
  69.    for(i=0; i<100000; i++)
  70.    {
  71.       ReadEClock(&time2);
  72.       TimeDiff(&time1, &time2);
  73.    }
  74.  
  75.    ReadEClock(&time2);
  76.    timeover = TimeDiff(&time1, &time2);
  77.    
  78.    printf("Timer overhead is %ld/%f (%ld milliseconds more than loop)\n", timeover,
  79.       (double)timeover/100000,
  80.       timeover-loopover);
  81.  
  82.    return 0;
  83. }
  84. #endif
  85.